package com.xl.rentkeeper.msg;

import com.squareup.wire.Wire;
import com.xl.fuse.protos.msg.Msg;
import com.xl.rentkeeper.db.orm.DBObj;
import com.xl.rentkeeper.db.orm.DBObjBase;
import com.xl.rentkeeper.db.orm.dsl.ConflictClause;
import com.xl.rentkeeper.db.orm.dsl.Ignore;
import com.xl.rentkeeper.db.orm.dsl.UniqueConstraints;

/**
 * Created by zwc on 2015/7/24.
 */
@UniqueConstraints(columnNames = "seq", clause = ConflictClause.REPLACE)
public class Message extends DBObjBase implements DBObj{
    static Wire wire = new Wire();
    public long seq;
    public long fromUin;
    public long toUin;
    public long msgType;
    public long msgTime;
    public long receivingTime;
    public String netState;
    public byte[] data;
    @Ignore
    public Msg msg;

    @Override
    public void preSave() {
        if (msg != null) {
            data = msg.toByteArray();
        } else {
            data = null;
        }
    }

    @Override
    public void afterLoad() {
        msg = null;
        if (data != null) {
            try {
                msg = wire.parseFrom(data,Msg.class);
            } catch (Exception e) {
                e.printStackTrace();
                msg = null;
            }
        }
    }
}
